package com.yanzhu.api;

import com.yanzhu.bean.ResponseBean;
import com.yanzhu.constant.Constant;
import com.yanzhu.service.LikeService;
import com.yanzhu.service.UserService;
import com.yanzhu.service.VideoService;
import com.yanzhu.service.model.Message;
import com.yanzhu.service.model.Upvote;
import com.yanzhu.service.model.User;
import com.yanzhu.service.model.Video;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author: 方炎柱
 * @Description: 点赞处理
 * @Date: Created in 22:02 2018/1/21
 * @Modified:
 */
@Log4j2
@RestController
@PropertySource("classpath:config/config.properties")
public class LikeAPI {

    @Autowired
    private LikeService likeService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private UserService userService;

    @Value("${like.fee}")
    private float like_fee;
    //点赞
    @RequestMapping(value="/likeVideo")
    public ResponseBean likeVideo(HttpServletRequest request) throws Exception {
        String uid = request.getParameter("uid");
        String vid = request.getParameter("vid");

        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数!");

        if(vid == null || vid.equals(""))
            return ResponseBean.fail("缺少视频ID参数!");

        String like_sum = request.getParameter("count");
        if(like_sum == null || like_sum.equals(""))
            return ResponseBean.fail("缺少点赞次数参数!");

        Video video = videoService.selectById(Long.parseLong(vid));
        if(video == null)
            return ResponseBean.fail("该视频不存在!");

        User user = userService.queryUserInfo(Long.parseLong(uid));
        if(user == null)
            return ResponseBean.fail("该改用户不存在!");

        Upvote upvote = new Upvote();
        upvote.setUid(Long.parseLong(uid));
        upvote.setVid(Long.parseLong(vid));
        upvote.setLike_time((int)(new Date().getTime()/1000));
        Message message = null;
        int count = likeService.queryLikeCount(video.getId(), user.getId());
        if(count < 15 && ((count + Integer.parseInt(like_sum)) >= 15)){
            message = new Message();
            message.setUid(video.getUid());
            message.setVid(video.getId());
            message.setOid(user.getId());
            message.setName(user.getNickname());
            message.setContent(Constant.MESSAGE_CONTENT4);
            message.setType(Constant.MESSAGE_TYPE2);
            message.setAvatar(user.getAvatar());
            message.setImage_path(video.getImage_path());
            message.setMessage_time((int)(new Date().getTime()/1000));
        }

        likeService.likeVideo(upvote, Integer.parseInt(like_sum), like_fee, message);

        return ResponseBean.ok();
    }

    @RequestMapping(value="/queryVideoLikeCount")
    public ResponseBean queryVideoLikeCount(HttpServletRequest request){
        String vid = request.getParameter("vid");
        if(vid == null || vid.equals(""))
            return ResponseBean.fail("缺少用户ID参数!");

        return ResponseBean.ok(likeService.queryVideoLikeCount(Long.parseLong(vid)));
    }


    @RequestMapping(value="/queryLikeList")
    public ResponseBean queryLikeList(HttpServletRequest request){
        String uid = request.getParameter("uid");
        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数!");

        return ResponseBean.ok(likeService.queryLikeList(Long.parseLong(uid)));
    }

    @RequestMapping(value="/deleteLikeById")
    public ResponseBean deleteById(HttpServletRequest request){
        String id = request.getParameter("id");
        if(id == null || id.equals(""))
            return ResponseBean.fail("缺少视频ID参数!");

        if(likeService.deleteLikeById(Long.parseLong(id)) < 0)
            return ResponseBean.fail("删除失败!");
        return ResponseBean.ok();
    }
}
